package com.cqjtu.graduation.controller;

import com.cqjtu.graduation.entity.DinnerTableDO;
import com.cqjtu.graduation.entity.FoodTypeDO;
import com.cqjtu.graduation.entity.OrderDO;
import com.cqjtu.graduation.entity.UserDO;
import com.cqjtu.graduation.service.DinnerTableDOService;
import com.cqjtu.graduation.service.FoodTypeDOService;
import com.cqjtu.graduation.service.OrderDOService;
import com.cqjtu.graduation.util.OrderIdGenerate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * spingmvc控制器
 * @author yhl
 */
@Controller
@RequestMapping("/order")
public class OrderController extends BaseController {
    @Autowired
    private OrderDOService orderDOService;
    @Autowired
    private FoodTypeDOService foodTypeDOService;
    @Autowired
    private DinnerTableDOService dinnerTableDOService;
    @RequestMapping("/addorder")
    public String addOrder(HttpServletRequest request){
        List<DinnerTableDO> tables = dinnerTableDOService.listDinnerDO();
        List<FoodTypeDO> result = foodTypeDOService.findAllFoodTypeInfo();
        request.setAttribute("foodtypes",request);
        request.setAttribute("tables",tables);
        return "OrderSys/admin/LookClassOfFood";
}

    /**
     * 生成订单
     * @param order
     * @param request
     * @return
     */
    @RequestMapping("/doaddorder")
    public String doAddOrder(OrderDO order , HttpServletRequest request){
        order.setOrderId(new OrderIdGenerate().orderIdGenerate());
        orderDOService.addOrderDO(order);
        return listOrder(request,order);
    }

    /**
     * 分页列举订单
     * @return
     */
    @RequestMapping("/listorder")
    public String listOrder(HttpServletRequest request , OrderDO orderDO){
        init(request);
        if(orderDO == null){
            orderDO = new OrderDO();
        }
        if(orderDO.getUserDO() == null){
            orderDO.setUserDO(new UserDO());
        }
        if(orderDO.getDinnerTableDO() == null){
            orderDO.setDinnerTableDO(new DinnerTableDO());
        }
        String userId = orderDO.getUserDO().getUserId();
        request.setAttribute("userId",userId);
        orderDO.getUserDO().setUserId(userId);
        orderDO.setStart(getStart());
        orderDO.setLength(PAGE_NUM);
        List<OrderDO> orders = orderDOService.listOrderDONoPaid(orderDO);
        Long total = orderDOService.conuntNoPaidOrderByCondition(orderDO);
        System.out.println(orders);
        request.setAttribute("orders",orders);
        request.setAttribute("total",total);
        return "OrderSys/admin/SettlementOrders";
    }
    @RequestMapping("/payoff")
    public String payOff(HttpServletRequest request,OrderDO orderDO){
        OrderDO condition = orderDOService.getOrderById(orderDO);
        orderDOService.payOffOrder(condition);
        return listOrder(request,new OrderDO());
    }
    @RequestMapping("/showOrderFoodStatus")
    public String showOrderFoodStatus(HttpServletRequest request){
        init(request);
        List<OrderDO> dos = orderDOService.findTableNumber();
        request.setAttribute("tlist",dos);
//        List<FoodTypeListDO> foodTypeListDOS;
//        String flist=null;
//       /* for (int i = 0; i <dos.size() ; i++) {
//            foodTypeListDOS = orderDOService.findFoodType(dos.get(i).getOrderId());
//           // flist="list"+"_"+dos.get(i).getOrderTableNumber();
//            request.setAttribute("flist",foodTypeListDOS);
//            System.out.println(foodTypeListDOS);
//        }*/
        return "OrderSys/kichen/CookShowPushMessage";
    }
}
